var initChart = function (basePath,params) {

    var sourceConfig = {
        //趋势图
        sales:basePath+'/manage/home/sales',
        //利润总数
        profit:basePath+'/manage/home/profit',
        //累计订单
        orderSum:basePath+'/manage/home/orderSum',
        //粉丝总数
        fans:basePath+'/manage/home/fans',
        //预付金总额
        deposit:basePath+'/manage/home/deposit',
        //充值余额
        balance:basePath+'/manage/home/balance',
        //时长卡总额
        timecard:basePath+'/manage/home/timecard',
        //雨伞情况统计
        umbrellaStatistics:basePath+'/manage/home/umbrellaStatistics',
        //设备情况统计
        machineStatistics:basePath+'/manage/home/machineStatistics',
    };

    // 路径配置
    require.config({
        paths: {
            echarts: basePath+'/resources/zheng-admin/plugins/echarts-2.2.7/build/dist'
        }
    });
// 使用
    require(
        [
            'echarts',
            'echarts/chart/pie',
            'echarts/chart/line'// 使用柱状图就加载pie模块，按需加载
        ],
        function (ec) {

            initLineChart(ec,"sales_area",sourceConfig.sales,params,"销售趋势图");

            initPieChart(ec,"profit_pie",sourceConfig.profit,params,"利润总额",'￥');

            initPieChart(ec,"order_pie",sourceConfig.orderSum,params,"累计订单",'笔');

            initPieChart(ec,"fans_pie",sourceConfig.fans,params,"粉丝总数",'位');

            initPieChart(ec,"deposit_pie",sourceConfig.deposit,params,"预付金总额",'￥');

            initPieChart(ec,"balance_pie",sourceConfig.balance,params,"充值余额",'￥');

            initPieChart(ec,"timecard_pie",sourceConfig.timecard,params,"时长卡总额",'￥');

            initBasePieChart(ec,"umbrella_statistics",sourceConfig.umbrellaStatistics,params,"雨伞情况");

            initBasePieChart(ec,"machine_statistics",sourceConfig.machineStatistics,params,"设备情况");

        }
    );
}

var initPieChart = function(echart,id,url,reqData,title,subText){
    if(!document.getElementById(id))return;
    $.ajax({dataType: 'json',
        type: 'get',
        url: url,
        data: reqData,
        success: function(result) {
            // 基于准备好的dom，初始化echarts图表
            var myChart = echart.init(document.getElementById(id));
            var option = {
                title: {
                    text: result,
                    subtext: subText,
                    x: 'center',
                    y: 'center',
                    itemGap: 5,
                    textStyle : {
                        color : 'rgba(30,144,255,0.8)',
                        fontFamily : '微软雅黑',
                        fontSize : 20,
                        fontWeight : 'bolder'
                    }
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{b} : ￥{c} ({d}%)",
                    position : function(p) {
                        // 位置回调
                        return [p[0] + 10, p[1] - 15];
                    },
                },
                calculable : true,
                series : [
                    {
                        name:title,
                        type:'pie',
                        radius : ['65%', '85%'],
                        itemStyle : {
                            normal : {
                                label : {
                                    show : false
                                },
                                labelLine : {
                                    show : false
                                }
                            },
                            emphasis : {
                                label : {
                                    show : false,
                                    formatter : "{d}%",
                                    position : 'center',
                                    textStyle : {
                                        fontSize : '30',
                                        fontWeight : 'bold'
                                    }
                                }
                            }
                        },
                        data:[
                            {value:result, name:title}
                        ]
                    }
                ]
            };
            myChart.setOption(option);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        }
    });
}

var initBasePieChart = function(echart,id,url,reqData,title){
    if(!document.getElementById(id))return;
    $.ajax({dataType: 'json',
        type: 'get',
        url: url,
        data: reqData,
        success: function(result) {
            // 基于准备好的dom，初始化echarts图表
            var myChart = echart.init(document.getElementById(id));
            var  option = {
                // title : {
                //     text: title,
                //     x:'center'
                // },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'horizontal',
                    x: 'center',
                    data:result.legend
                },
                toolbox: {
                    show : false,
                    feature : {
                        mark : {show: true},
                        dataView : {show: true, readOnly: false},
                        magicType : {
                            show: true,
                            type: ['pie', 'funnel'],
                            option: {
                                funnel: {
                                    x: '25%',
                                    width: '50%',
                                    funnelAlign: 'left',
                                    max: 1548
                                }
                            }
                        },
                        restore : {show: true},
                        saveAsImage : {show: true}
                    }
                },
                calculable : true,
                series : [
                    {
                        itemStyle : {
                            normal : {
                                label : {
                                    show : false
                                },
                                labelLine : {
                                    show : false
                                }
                            },
                        },
                        name:result.name,
                        type:'pie',
                        radius : '55%',
                        // center: ['62%', '70%'],
                        data:result.series
                    }
                ]
            };
            myChart.setOption(option);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        }
    });


}

var initLineChart = function(echart,id,url,reqData,title){
    if(!document.getElementById(id))return;
    $.ajax({dataType: 'json',
        type: 'get',
        url: url,
        data: reqData,
        success: function(result) {
            // 基于准备好的dom，初始化echarts图表
            var myChart = echart.init(document.getElementById(id));
            var  option = {
                tooltip : {
                    trigger: 'axis'
                },
                legend: {
                    data:result.legend
                },
                toolbox: {
                    show : false,
                    feature : {
                        mark : {show: true},
                        dataView : {show: true, readOnly: false},
                        magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                        restore : {show: true},
                        saveAsImage : {show: true}
                    }
                },
                calculable : true,
                xAxis : [
                    {
                        type : 'category',
                        boundaryGap : false,
                        data : result.category
                    }
                ],
                yAxis : [
                    {
                        type : 'value'
                    }
                ],
                series : result.series.map(function(d){
                    return {
                        name:d.name,
                        type:'line',
                        stack: '',
                        itemStyle: {normal: {areaStyle: {type: 'default'}}},
                        data:d.data
                    }
                })
            };
            myChart.setOption(option);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        }
    });
}